package org.javaboy.vhr.exception;

import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;
import org.javaboy.vhr.responseBean.ServerResponse;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;

/**
 * 全局异常处理
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(SQLException.class)
    public ServerResponse sqlException(SQLException e){
        e.printStackTrace();
        if(e instanceof MySQLIntegrityConstraintViolationException){
            return ServerResponse.error(500,"该数据有关联数据,操作失败");
        }
        return ServerResponse.error(500,"数据库异常");
    }
}
